<?php

namespace statistics\controller\editor;

use statistics\controller\AbstractPageController;
use statistics\utils\AccessControlManager;
use statistics\view\renderers\editor\FormStatisticRenderer;
use statistics\utils\StatisticPersistenceService;

require_once(substr(__DIR__, 0, strpos(__DIR__, 'statistics')+10). DIRECTORY_SEPARATOR .'includes.php');

class EditorController extends AbstractPageController{
		
	private $requiredPrivilege;
	private $currentStatistic;
	private $allStatisticConfigurations;
	private $currentStatisticID;
	private $statPersistService;

	public function __construct(){
		parent::__construct('Statistik-Editor');
		$this->requiredPrivilege = AccessControlManager::CONFIG_PRIVILEGE;
		$this->statPersistService = StatisticPersistenceService::getInstance();	
	}

	protected function getRequiredPrivilege(){
		return $this->requiredPrivilege;
	}
	
	protected function processPageData (){
		global $gCurrentOrganization;
		$this->currentStatistic = $this->statPersistService->getStatistic($this->currentStatisticID);	
		$currentOrganization = $gCurrentOrganization->getValue('org_id','');
		$this->allStatisticConfigurations = $this->statPersistService->loadAllStatisticConfigsArray($currentOrganization);
	}
	
	protected function getRenderableContent(){
		$formStatisticRenderer = new FormStatisticRenderer($this->currentStatistic,$this->allStatisticConfigurations);
		$this->getPageRenderer()->addPageHeader($formStatisticRenderer->getPageHeader());
		return $formStatisticRenderer;
	}
	
	protected function readHTMLParameters() {
		if (isset ( $_GET ['sta_id'] )) { // falls Statistik-ID gesetzt, vorhandene Konfiguration auslesen
			$this->paramReader->setDataType('numeric');
			$this->paramReader->setDefaultValue(1);
			$this->currentStatisticID = $this->paramReader->readGETParameter('sta_id');
		} else {
			header ( 'Location: ' . $this->scrollPosHandler->addScrollPosToURL(STATISTICS_WEB_ROOT.'/controller/editor/editor_process.php?mode=load'));
		}
	}
	
}
?>